package com.test.test.thread;

public class WiretReadDemo {
	
	private boolean reading = false;
	private Integer num1 = 1;
	private Integer num2 =3;
	private Integer result =0;
	public synchronized void read(){
		reading = true;
		num1 = 2;
	}
	
	public synchronized void wirte(){
		if(reading){
			result = num1 * 3;
		}
		System.out.println("计算结果为"+result);
	}
	public static void main(String[] args) throws InterruptedException {
		for (int i = 0; i < 20; i++) {
			WiretReadDemo demo = new WiretReadDemo();
			demo.new ReadWirteTest(true).start();
			Thread.sleep(100);
			demo.new ReadWirteTest(false).start();
		}
	}
	private class ReadWirteTest extends Thread{
		
		private boolean flag;
		public ReadWirteTest() {
			// TODO Auto-generated constructor stub
		}
		public ReadWirteTest(boolean flag) {
			// TODO Auto-generated constructor stub
			this.flag = flag;
		}
		@Override
		public void run() {
			// TODO Auto-generated method stub
			if(flag){
				read();
			}else{
				wirte();
			}
		}
	}
	
	
}
